package logic

import (
	"context"
	"fmt"
	"hema-fresh/hemarpc/hemarpc"
	"hema-fresh/hemarpc/internal/svc"
	"hema-fresh/model/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductListLogic {
	return &ProductListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductListLogic) ProductList(in *hemarpc.ProductListRequest) (*hemarpc.ProductListResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	mysql.DB.Debug().Where("id = ?", in.ProductId).Find(&product)

	if product.ID == 0 {
		return nil, fmt.Errorf("当前图书不存在,查看失败")
	}

	return &hemarpc.ProductListResponse{
		Name:      product.Name,
		Price:     float32(product.Price),
		ClassId:   int64(product.ClassId),
		ProductId: int64(product.ID),
		CreatedAt: product.CreatedAt.Format(time.DateTime),
	}, nil
}
